/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.rentalofconstructionmachineryweb.controller;

import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.MachineDTO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.RevisionDTO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.service.MachineService;
import cz.muni.fi.pa165.rentalofconstructionmachinery.service.RevisionService;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author stefan
 */
@Controller
@RequestMapping("/revision")
public class RevisionController {

    private RevisionService revisionService;
    private MachineService machineService;

    @RequestMapping("")
    public String accountList(Model model) {
        model.addAttribute("revisions", revisionService.getAll());
        return "revision";
    }
    
    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public String getNew(Model model) {
        List<MachineDTO> machines = machineService.listMachines();
        if(0 == machines.size()) return "redirect:/revision?error&msg=No machine found";
        model.addAttribute("machines", machines);
        return "revisionForm";
    }
    
    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public String postNew(@RequestParam("date") String dateStr, @RequestParam("comment") String comment, @RequestParam("machineId") Long machineId, Model model) {
        if(null == dateStr || 0 == dateStr.length() || null == comment || 0 == comment.length() || null == machineId) {
            return "redirect:/revision?error&msg=Some fields not correctly populated";
        } else {
            MachineDTO machine = machineService.searchForMachineById(machineId);
            if(null == machine) return "redirect:/revision?error&msg=Machine with given Id doesn't exist";

            Date date;
            try {
                date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
            } catch (Exception ex) {
                return "redirect:/revision?error&msg=Bad date format";
            }

            RevisionDTO newRevision = new RevisionDTO();
            newRevision.setMachine(machine);
            newRevision.setDate(date);
            newRevision.setComment(comment);
            revisionService.create(newRevision);

            return "redirect:/revision?success";
        }
    }
    
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public String getEdit(@PathVariable Long id, Model model) {
        if(null == id) return "redirect:/revision?error&msg=Revision Id not provided";
        RevisionDTO revision = revisionService.getRevisionById(id);
        if(null == revision) return "redirect:/revision?error&msg=Revision with given Id doesn't exist";
        model.addAttribute("machines", machineService.listMachines());
        model.addAttribute("revision", revision);
        return "revisionForm";
    }
    
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.POST)
    public String postEdit(@PathVariable Long id, @RequestParam("date") String dateStr, @RequestParam("comment") String comment, @RequestParam("machineId") Long machineId, Model model) {
        if(null == dateStr || 0 == dateStr.length() || null == comment || 0 == comment.length() || null == machineId || null == id) {
            return "redirect:/revision?error&msg=Some fields not correctly populated";
        } else {
            RevisionDTO revision = revisionService.getRevisionById(id);
            if(null == revision) return "redirect:/revision?error&msg=Revision with given Id doesn't exist";
            MachineDTO machine = machineService.searchForMachineById(machineId);
            if(null == machine) return "redirect:/revision?error&msg=Machine with given Id doesn't exist";

            Date date;
            try {
                date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
            } catch (Exception ex) {
                return "redirect:/revision?error&msg=Bad date format";
            }

            revision.setMachine(machine);
            revision.setDate(date);
            revision.setComment(comment);
            revisionService.update(revision);

            return "redirect:/revision?success";
        }
    }
    
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable Long id, Model model) {
        if(null == id) return "redirect:/revision?error&msg=Revision Id not provided";
        RevisionDTO revision = revisionService.getRevisionById(id);
        if(null == revision) return "redirect:/revision?error&msg=Revision with given Id doesn't exist";
        revisionService.delete(revision);
        return "redirect:/revision?success";
    }

    @Autowired
    public RevisionController(RevisionService revisionService, MachineService machineService) {
        this.revisionService = revisionService;
        this.machineService = machineService;
    }
}